home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_12_04
/
janzen
/
janzen.fzy
< prev
Wrap
Text File
|
1994-03-08
|
21KB
|
817 lines
// window.cxx
// This is window.cxx, a C++ implementation of Bart Kosko's fuzzy associative memory
// and also fuzzy values, fuzzy sets and fuzzy membership functions for fuzzy logic.
// This software is provided without warranty and with no liability by
// Thomas E. Janzen
// 208A Olde Derby Rd
// Norwood, MA 02062
// tej@world.std.com
//
// This is an improved version of code provided in C Users Journal November 1993.
// 12 December 93
// To create this file, the examples in the article were merged in order by example
// number; then changes were made.
// #include directives repeated in each example were removed from examples 2,3,4,5.
// One error found in the article code was line 35 in example 4. The membership
// member function was declared with an implied int argument.
// This should be a fuzzy.
// Other changes made here:
// exit(0) was replaced with return 0L because of a compiler requirement for a return
// from main()
// "partly closed" was completely removed from the memberships for window position.
// It was probably conceptually misled.
// Since the closed position of the window is at zero inches, and near the end
// the fuzzy membership is membership is multiplied by zero, the "closed"
// membership object doesn't do much, but in the divisor in the third-to-last
// statement it has some effect.
// Some inline modifiers were added to a few functions.
// Comments were added to explain the numeric values used.
// Command line arguments are used. Type window [temperature]. If temperature
// is omitted, a prompt for it will appear.
// The numeric values for window position were changed in the membership functions.
// The window opening now seems more or less reasonable. The idea was to open
// the window only in mild weather, and to close it in cold and hot weather.
//
// Fuzzy references:
//
// Klir, George J.; Folger, Tina A. 1988. Fuzzy Sets, Uncertainty,
// and Information. Prentice Hall. Englewood Cliffs.
// Kosko, Bart. 1992. Neural Networks and Fuzzy Systems, A
// Dynamical Systems Approach to Machine Intelligence. Prentice
// Hall. Englewood Cliffs.
// Results:
// For temperature: 0 open the window 0 inches
// For temperature: 10 open the window 2.25728 inches
// For temperature: 20 open the window 7.5 inches
// For temperature: 30 open the window 7.5 inches
// For temperature: 40 open the window 7.5 inches
// For temperature: 50 open the window 9.82819 inches
// For temperature: 60 open the window 10.9489 inches
// For temperature: 70 open the window 10.9489 inches
// For temperature: 80 open the window 7.5 inches
// For temperature: 90 open the window 0 inches
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
class fuzzy
{
private:
double truth;
public:
inline fuzzy(const double);
void get(double *);
inline double get(void) const;
inline fuzzy operator!(void) const;
inline fuzzy very(void) const;
inline fuzzy somewhat(void) const;
inline fuzzy& operator|=(fuzzy&);
inline fuzzy& operator&=(fuzzy&);
fuzzy& operator=(const fuzzy&);
fuzzy& operator=(const double);
friend fuzzy operator|(const fuzzy&,
const fuzzy&);
friend fuzzy operator&(const fuzzy&,
const fuzzy&);
friend fuzzy fuzzy_implies(const fuzzy&,
const fuzzy&);
friend fuzzy fuzzy_iff(const fuzzy&,
const fuzzy&);
friend inline int operator<(const fuzzy&,
const fuzzy&);
friend inline int operator<=(const fuzzy&,
const fuzzy&);
friend inline int operator==(const fuzzy&,
const fuzzy&);
friend inline int operator>=(const fuzzy&,
const fuzzy&);
friend inline int operator>(const fuzzy&,
const fuzzy&);
friend inline int operator!=(const fuzzy&,
const fuzzy&);
friend ostream& operator<<(ostream& s,
const fuzzy& z);
};
inline fuzzy::fuzzy(const double temp_truth = 0.0)
{
truth = ((temp_truth >= 0.0)
&& (temp_truth <= 1.0)) ?
temp_truth : 0.0;
return;
}
inline void fuzzy::get(double *get_truth)
{
*get_truth = truth;
return;
}
double fuzzy::get(void) const
{
return truth;
}
inline fuzzy fuzzy::operator!(void) const
{
return (1.0 - truth);
}
inline fuzzy fuzzy::very(void) const
{
return (truth * truth);
}
inline fuzzy fuzzy::somewhat(void) const
{
return sqrt(truth);
}
inline fuzzy& fuzzy::operator|=(fuzzy& value)
{
return (*this = *this | value);
}
inline fuzzy& fuzzy::operator&=(fuzzy& value)
{
return (*this = *this & value);
}
fuzzy& fuzzy::operator=(const fuzzy& fuzzy_in)
{
truth = fuzzy_in.truth;
return *this;
}
fuzzy& fuzzy::operator=(const double dbl_in)
{
truth = ((dbl_in >= 0.0) && (dbl_in <= 1.0)) ?
dbl_in : 0.0;
return *this;
}
fuzzy operator|(const fuzzy& value_a, const fuzzy& value_b)
{
return (value_a.truth > value_b.truth) ?
value_a : value_b;
}
fuzzy operator&(const fuzzy &value_a,
const fuzzy &value_b)
{
return (value_a < value_b) ? value_a : value_b;
}
fuzzy fuzzy_implies(const fuzzy& value_a,
const fuzzy& value_b)
{
auto double temp_value;
temp_value = 1.0 - value_a.truth + value_b.truth;
return ((temp_value < 1.0) ? temp_value : 1.0);
}
fuzzy fuzzy_iff(const fuzzy& value_a,
const fuzzy& value_b)
{
return 1.0 - fabs(value_a.truth - value_b.truth);
}
inline int operator<(const fuzzy& value_a,
const fuzzy& value_b)
{
return (value_a.truth < value_b.truth);
}
inline int operator<=(const fuzzy& value_a,
const fuzzy& value_b)
{
return (value_a.truth <= value_b.truth);
}
inline int operator==(const fuzzy& value_a,
const fuzzy& value_b)
{
return (value_a.truth == value_b.truth);
}
inline int operator>=(const fuzzy& value_a,
const fuzzy& value_b)
{
return (value_a.truth >= value_b.truth);
}
inline int operator>(const fuzzy& value_a,
const fuzzy& value_b)
{
return (value_a.truth > value_b.truth);
}
inline int operator!=(const fuzzy& value_a,
const fuzzy& value_b)
{
return (value_a.truth != value_b.truth);
}
ostream& operator<<(ostream& s, const fuzzy& z)
{
return s << z.truth;
}
istream& operator>>(istream& s, fuzzy& fur)
{
auto double lint = 0.0;
s >> lint;
fur = lint;
return s;
}
class fzy_set
{
private:
int dimension;
fuzzy *fzy_data;
public:
fzy_set(const int);
fzy_set(const fzy_set&);
~fzy_set(void);
fzy_set& operator=(const fzy_set&);
fuzzy& operator[](const int len);
fuzzy& operator[](const int len) const;
friend int operator<(const fzy_set&,
const fzy_set&);
friend int operator<=(const fzy_set&,
const fzy_set&);
friend int operator==(const fzy_set&,
const fzy_set&);
friend int operator>=(const fzy_set&,
const fzy_set&);
friend int operator>(const fzy_set&,
const fzy_set&);
friend int operator!=(const fzy_set&,
const fzy_set&);
friend ostream& operator<<(ostream& s,
fzy_set& z);
friend fuzzy fzy_set_max(const fzy_set&);
friend fzy_set operator*(const fzy_set&,
const class fam_2&);
};
fzy_set::fzy_set(const int size = 1) // constructor
{
dimension = ((size >= 0) ? size : 1);
fzy_data = new fuzzy[dimension];
return;
}
fzy_set::f